package com.wei.ds;

import java.time.*;
import java.util.concurrent.*;

public class SchedulerTest {
    private static ScheduledExecutorService startTaskExecutor;
    private Future openFuture;
    private Future closeFuture;
    private static LocalDateTime exceptStartDate;
    private static LocalDateTime exceptStopDate;
    private static LocalDateTime lastStartDate;
    private static LocalDateTime lastStopDate;
    private static String state = "";

    public void start(LocalDateTime date) {
        long input = date.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

        long delay = input - now;
        openFuture = startTaskExecutor.schedule(() -> {
            state = "Start";
            lastStartDate = LocalDateTime.now(ZoneId.of("+8"));
            System.out.println("start -------------> "+lastStartDate);
        }, delay, TimeUnit.MILLISECONDS);
        exceptStartDate=date;
    }

    public void stop(LocalDateTime date){
        long input = date.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long now = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

        long delay = input - now;
        closeFuture = startTaskExecutor.schedule(() -> {
            state = "Close";
            lastStopDate = LocalDateTime.now(ZoneId.of("+8"));
            System.out.println("end -------------> "+lastStopDate);
        }, delay, TimeUnit.MILLISECONDS);
        exceptStopDate=date;
    }

    public void update(LocalDateTime start,LocalDateTime end){
        LocalDateTime now = LocalDateTime.now();
        if(start.compareTo(now)<=0||end.compareTo(now)<=0){
            System.err.println("日期不正确,不得早于当前日期");
            return;
        }
        if(exceptStartDate.compareTo(now)<=0||exceptStopDate.compareTo(now)<=0){
            System.err.println("日期不正确,已经停止或启动");
            return;
        }
        openFuture.cancel(true);
        closeFuture.cancel(true);
        runTask(start,end);
    }

    public void runTask(LocalDateTime start,LocalDateTime end){
        start(start);
        stop(end);
    }



    public SchedulerTest() {
        if (startTaskExecutor == null ){
            this.startTaskExecutor = new ScheduledThreadPoolExecutor(4);
        }
    }

    public static LocalDateTime getExceptStartDate() {
        return exceptStartDate;
    }

    public static void setExceptStartDate(LocalDateTime exceptStartDate) {
        SchedulerTest.exceptStartDate = exceptStartDate;
    }

    public static LocalDateTime getExceptStopDate() {
        return exceptStopDate;
    }

    public static void setExceptStopDate(LocalDateTime exceptStopDate) {
        SchedulerTest.exceptStopDate = exceptStopDate;
    }

    public static LocalDateTime getLastStartDate() {
        return lastStartDate;
    }

    public static void setLastStartDate(LocalDateTime lastStartDate) {
        SchedulerTest.lastStartDate = lastStartDate;
    }

    public static LocalDateTime getLastStopDate() {
        return lastStopDate;
    }

    public static void setLastStopDate(LocalDateTime lastStopDate) {
        SchedulerTest.lastStopDate = lastStopDate;
    }

    public static String getState() {
        return state;
    }

    public static void setState(String state) {
        SchedulerTest.state = state;
    }
}
